﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DomainDriver.Presentation.Silverlight.Base;
using AutomotivePricingModel.Companies;
using AutomotivePricingSilverlightApp.Web.Services;

namespace AutomotivePricingSilverlightApp.Controls.Companies
{
    public partial class CompanyListView : UserControl, IDomainView
    {
        private CompanyListController m_Controller;
        private bool m_UpdatingDisplay = false;

        public CompanyListView()
        {
            InitializeComponent();
            m_Controller = new CompanyListController();
            m_Controller.View = this;
            Loaded += new RoutedEventHandler(CompanyListView_Loaded);
            TypedController.ApplicationObject.StateContainer.StateChanged += new StateChangeHandler(StateContainer_StateChanged);
            TypedController.RefreshRequested += new RefreshRequestHandler(TypedController_RefreshRequested);
        }

        private void CompanyListView_Loaded(object sender, RoutedEventArgs e)
        { DomainViewExtensions.ValidateAndRefresh(this); }

        private void StateContainer_StateChanged(object sender, StateChangeEventArgs args)
        {
            if (sender == Controller)
            { return; }

            if (args.StateName != CompanyListController.CompanySelectionName)
            { return; }

            DomainViewExtensions.ValidateAndRefresh(this);
        }

        public IDomainController Controller
        {
            get { return m_Controller; }
        }

        public CompanyListController TypedController
        {
            get { return m_Controller; }
        }

        public void LockView()
        {
            // do nothing
        }

        public void UnlockView()
        {
            // do nothing
        }

        public void ClearView()
        {
            m_UpdatingDisplay = true;
            m_CompanyListBox.ItemsSource = null;
            m_UpdatingDisplay = false;
        }

        public void ReloadView()
        { TypedController.GetCompanyListAsync(); }

        private void TypedController_RefreshRequested(object sender, RefreshRequestEventArgs args)
        {
            LoadOperation<Company> loadOperation = args.GetTypedCompletedOperation<LoadOperation<Company>>();
            if (loadOperation == null)
            {
                ClearView();
                return;
            }

            m_UpdatingDisplay = true;

            m_CompanyListBox.ItemsSource = loadOperation.Entities;
            m_CompanyListBox.DisplayMemberPath = "CompanyName";

            Company selectedCompany = TypedController.GetSelectedCompany();
            foreach (Company company in m_CompanyListBox.ItemsSource)
            {
                if (selectedCompany != null)
                {
                    if (company.EF_Key == selectedCompany.EF_Key)
                    {
                        m_CompanyListBox.SelectedItem = company;
                        m_Controller.SetSelectedCompany(company);
                    }
                }
            }

            if (m_CompanyListBox.SelectedItem == null)
            {
                if (loadOperation.Entities.Count<Company>() > 0)
                {
                    Company company = loadOperation.Entities.First<Company>();
                    m_CompanyListBox.SelectedItem = company;
                    m_Controller.SetSelectedCompany(company);
                }
            }

            m_UpdatingDisplay = false;
        }

        private void m_CompanyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (m_UpdatingDisplay)
            { return; }

            Company selectedValue = (Company)m_CompanyListBox.SelectedItem;
            TypedController.SetSelectedCompany(selectedValue);
        }
    }
}